<?php if(!defined('CALL')) exit('No direct script access allowed');

include_once('lib/utils.class.php');
include_once('lib/xtemplate.class.php');
include_once('lib/logger.class.php');

class vUsers
{
	private static $user  = null;
	private static $actor = null;
	
	public static function buildUserPage($actor, $user)
	{
		$male = (bool) ($user->props['male'] == "Y");
		$gender1 = $male ? "Он"  : "Она";
		$gender2 = $male ? "ним" : "ней";
		$gender3 = $male ? "Ему" : "Ей";
		
		self::$actor = $actor;
		self::$user = $user;
		
		$xtpl = new XTemplate('xtpl/main-users.xtpl');
		
		$html = "";
		
		$html .= self::buildUserList("$gender1 следит за:", $user->links["WM"], $xtpl);
		$html .= self::buildUserList("За $gender2 следят:", $user->links["WS"], $xtpl);
		$html .= self::buildUserList("$gender1 доверяет:", $user->links["FM"], $xtpl);
		$html .= self::buildUserList("$gender3 доверяют:", $user->links["FS"], $xtpl);
				
		$xtpl->parse('root');
		return $xtpl->text('root');
	}
	
	public static function buildUserList($title, $users, &$xtpl = null)
	{
		if(is_null($xtpl))
			$xtpl = new XTemplate('xtpl/main-users.xtpl');
			
		$xtpl->assign('list_title', $title);
		$xtpl->assign('root', ROOT_URL);
		if(count($users) > 0)
		{
			foreach($users as $id)
				self::buildUserBox($id, self::$user->users[$id], $xtpl);
		}
		else
		{
			$xtpl->assign('txt', 'Нет никого');
			$xtpl->parse('root.list.nothing');
		}
		$xtpl->parse('root.list');
		return $xtpl->text('root.list');
	}
	
	public static function buildSimpleList($title, $users, &$xtpl = null)
	{
		if(is_null($xtpl))
			$xtpl = new XTemplate('xtpl/main-users.xtpl');
			
		$xtpl->assign('list_title', $title);
		$xtpl->assign('root', ROOT_URL);
		if(count($users) > 0)
		{
			foreach($users as $user)
				self::buildUserBox($user['id'], $user, $xtpl);
		}
		else
		{
			$xtpl->assign('txt', 'Нет никого');
			$xtpl->parse('root.list.nothing');
		}
		$xtpl->parse('root.list');
		return $xtpl->text('root.list');
	}
	
	public static function buildUserBox($uid, $user, &$xtpl)
	{
		if(is_null($xtpl))
			$xtpl = new XTemplate('xtpl/main-users.xtpl');
			
		$xtpl->assign('user_id', $uid);
		$xtpl->assign('username', $user["username"]);
		$xtpl->assign('face', $user["face"]);
		$xtpl->assign('city', $user["city"]);
		$xtpl->parse('root.list.item');
		
		return $xtpl->text('root.list.item');
	}
}